package com.andy.blog.framework.manager.factory;

import com.andy.blog.common.constant.BaseConstants;
import com.andy.blog.common.enums.EmailSendTypeEnum;
import com.andy.blog.common.utils.LogUtils;
import com.andy.blog.common.utils.ServletUtils;
import com.andy.blog.common.utils.ip.AddressUtils;
import com.andy.blog.common.utils.ip.IpUtils;
import com.andy.blog.common.utils.spring.SpringBeanUtils;
import com.andy.blog.dal.service.HitokotoDalService;
import com.andy.blog.dal.service.SysLoginLogDalService;
import com.andy.blog.dal.service.SysOperLogDalService;
import com.andy.blog.dal.vo.HitokotoVO;
import com.andy.blog.dal.vo.SysLoginLogVO;
import com.andy.blog.dal.vo.SysOperLogVO;
import com.andy.blog.integration.service.MailIntegrationService;
import com.andy.blog.integration.vo.SimpleMailVO;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;

/**
 * 异步工厂
 * Created by andy on 2023-04-05 11:38
 */
public class AsyncFactory {
    private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    /**
     * 记录登录信息
     *
     * @param sysLoginLogVO 用户名
     * @param args          列表
     * @return 任务task
     */
    public static TimerTask recordLoginLog(final SysLoginLogVO sysLoginLogVO, final Object... args) {
        final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddress();
        return new TimerTask() {
            @Override
            public void run() {
                String address = AddressUtils.getRealAddressByIp2region(ip);
                String s = "[" + ip + "]" + address + LogUtils.getBlock(sysLoginLogVO.getUserName()) +
                        LogUtils.getBlock(sysLoginLogVO.getStatus()) + LogUtils.getBlock(sysLoginLogVO.getMsg());
                // 打印信息到日志
                sys_user_logger.info(s, args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                sysLoginLogVO.setIpAddress(ip);
                sysLoginLogVO.setLoginLocation(address);
                sysLoginLogVO.setBrowser(browser);
                sysLoginLogVO.setOs(os);
                // 日志状态
                if (StringUtils.equalsAny(sysLoginLogVO.getStatus(), BaseConstants.LOGIN_SUCCESS, BaseConstants.LOGOUT, BaseConstants.REGISTER)) {
                    sysLoginLogVO.setStatus(BaseConstants.SUCCESS);
                } else if (BaseConstants.LOGIN_FAIL.equals((sysLoginLogVO.getStatus()))) {
                    sysLoginLogVO.setStatus(BaseConstants.FAIL);
                }
                // 插入数据
                SpringBeanUtils.getBean(SysLoginLogDalService.class).insertLoginLog(sysLoginLogVO);
            }
        };
    }

    /**
     * 操作日志记录
     *
     * @param sysOperLogVO 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(final SysOperLogVO sysOperLogVO) {
        return new TimerTask() {
            @Override
            public void run() {
                // 远程查询操作地点
                sysOperLogVO.setOperLocation(AddressUtils.getRealAddressByIp2region(sysOperLogVO.getOperIp()));
                SpringBeanUtils.getBean(SysOperLogDalService.class).insert(sysOperLogVO);
            }
        };
    }

    /**
     * 邮件发送
     */
    public static TimerTask sendEmail(final SimpleMailVO simpleMailVO, final EmailSendTypeEnum emailSendTypeEnum) {
        return new TimerTask() {
            @Override
            public void run() {
                switch (emailSendTypeEnum) {
                    case SIMPLE_SEND:
                        SpringBeanUtils.getBean(MailIntegrationService.class).sendSimpleMail(simpleMailVO);
                        break;
                    case HTML_SEND:
                        SpringBeanUtils.getBean(MailIntegrationService.class).sendHtmlMail(simpleMailVO);
                        break;
                    case ATTACH_SEND:
                        SpringBeanUtils.getBean(MailIntegrationService.class).sendAttachmentsMail(simpleMailVO);
                        break;
                }
            }
        };
    }

    /**
     * 记录Hitokoto
     */
    public static TimerTask addHitokoto(final HitokotoVO hitokoto) {
        return new TimerTask() {
            @Override
            public void run() {
                SpringBeanUtils.getBean(HitokotoDalService.class).insertHitokoto(hitokoto);
            }
        };
    }
}
